package com.moxiao.yfjh.module.activity.service.productInfo;

import com.moxiao.yfjh.module.activity.dal.dataobject.productType.ProductTypeDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.productInfo.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.productInfo.ProductInfoConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.productInfo.ProductInfoMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲商品信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ProductInfoServiceImpl implements ProductInfoService {

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public Long createProductInfo(ProductInfoCreateReqVO createReqVO) {
        // 插入
        ProductInfoDO productInfo = ProductInfoConvert.INSTANCE.convert(createReqVO);
        productInfoMapper.insert(productInfo);
        // 返回
        return productInfo.getId();
    }

    @Override
    public void updateProductInfo(ProductInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductInfoExists(updateReqVO.getId());
        // 更新
        ProductInfoDO updateObj = ProductInfoConvert.INSTANCE.convert(updateReqVO);
        updateObj.setIsWriteoff(updateReqVO.getIsWriteoff());
        productInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductInfo(Long id) {
        // 校验存在
        validateProductInfoExists(id);
        // 删除
        productInfoMapper.deleteById(id);
    }

    private void validateProductInfoExists(Long id) {
        if (productInfoMapper.selectById(id) == null) {
            throw exception(PRODUCT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ProductInfoDO getProductInfo(Long id) {
        return productInfoMapper.selectById(id);
    }

    @Override
    public List<ProductInfoDO> getProductInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return productInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductInfoDO> getProductInfoPage(ProductInfoPageReqVO pageReqVO) {
        return productInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductInfoDO> getProductInfoList(ProductInfoExportReqVO exportReqVO) {
        return productInfoMapper.selectList(exportReqVO);
    }

    @Override
    public ProductInfoDO getProductInfoTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<ProductInfoDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(ProductInfoDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(ProductInfoDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return productInfoMapper.selectOne(lqw);
    }

    @Override
    public Integer getProductInfoNo(String activeNo) {
        return productInfoMapper.getProductInfoNo(activeNo);
    }

}
